package com.graduationpractice.dbot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduationpractice.dbot.biz.IUsercreatedatabaseService;
import com.graduationpractice.dbot.pojo.User;
import com.graduationpractice.dbot.pojo.Usercreatedatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @auther lidelong
 * @date 2022/3/24 9:49
 */
@Controller
public class IndexController {
    @Autowired
    private IUsercreatedatabaseService iUsercreatedatabaseService;

    /*设置默认登录页面为login页面*/
    @RequestMapping("/")
    public String toDefault()
    {
        return "login";
    }
    @RequestMapping("/login")
    public String toLogin(){return "login";}

    /*主页面*/
    @RequestMapping("/index")
    public String toIndex()
    {
        return "index";
    }

    /*用户信息*/
    @RequestMapping("UserInformation")
    public String toUserInformation(HttpSession session,Usercreatedatabase ucdb, Integer current,Integer size)
    {
        User user = (User)session.getAttribute("user");
        IPage<Usercreatedatabase> usercreatedatabaseIPage=new Page<>(current,size);
        IPage<Usercreatedatabase> usercreatedatabaseIPage1=this.iUsercreatedatabaseService.page(usercreatedatabaseIPage,new QueryWrapper<Usercreatedatabase>().eq("email",user.getEmail()));
        /*展示自己名下数据库*/
        /*用户页面展示自己的数据库*/
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++");
        List<Usercreatedatabase> usercreatedatabaseList=usercreatedatabaseIPage1.getRecords();
        long pagescount = usercreatedatabaseIPage1.getPages();
        System.out.println(usercreatedatabaseList);
        session.setAttribute("usercreatedatabaseList",usercreatedatabaseList);
        session.setAttribute("pagescount",pagescount);
        session.setAttribute("current",current);
        session.setAttribute("size",size);
        return "UserInformation";
    }

    /*数据库连接*/
    @RequestMapping("dblink")
    public  String todblink(HttpSession session)
    {
        session.removeAttribute("link_msg");
        return "dblink";
    }

    /*退出*/
    @RequestMapping("exit")
    public String Exit(HttpSession session) throws SQLException {
        //清除所有Session
        Connection connection=(Connection)session.getAttribute("connection");
        session.invalidate();
        if(connection!=null)
        connection.close();
        System.out.println("Session全部清除！");
        System.out.println("关闭数据库！");
        return "login";
    }

    /*新建查询清空session*/
    @RequestMapping("/newQuery")
    public String ToNewQuqery(HttpSession session){
        //跳转页面，清空session
        session.removeAttribute("tableName");
        session.removeAttribute("fieldName");
        session.removeAttribute("pojolist");
        session.removeAttribute("modifyStr");
        session.removeAttribute("failStr");
        session.removeAttribute("errorPrompt");
        return "newQuery";
    }

    /*进入表*/
    @RequestMapping("table")
    public  String toTable()
    {
        return "table";
    }

    //    跳转到注册页面
    @RequestMapping("/register")
    public String toRegister() {
        return "/register";
    }

    //    跳转到重置密码页面
    @RequestMapping("/findpassword")

    public String toFindPassword() {
        return "/findpassword";
    }

    //    跳转到重置密码页面
    @RequestMapping("/checkvercode")
    public String toCheckVerCode() {
        return "/checkvercode";
    }

    //    跳转到修改密码页面
    @RequestMapping("/updatepassword")
    public String toUpdatePassword() {
        return "/updatepassword";
    }



    //    跳转到404页面
    @RequestMapping("/404")
    public String to404() {
        return "/404";
    }

    //    跳转到新建用户表页面
    @RequestMapping("/createtable")
    public String toCreateTable() {
        return "/createtable";
    }


}
